package Client;

import java.util.HashMap;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Brandon
 */
public class Parser {
    
    public Parser() {
        
    }
    
    public HashMap<String, String[]> parse(String line){
        HashMap<String, String[]> command = new HashMap<String, String[]>();
        //PLAYERS
        if(line.startsWith("SVR PLAYERLIST")){
            line = line.substring(15);
            
        }else if (line.startsWith("OK") && Command.getLastCommand() == "login") {
           	 String[] values = {"OK"};
           	 command.put("LOGIN", values);
           	 return command;     
        //COMMANDO
        } else if(line.startsWith("SVR GAME")){
            line = line.substring(8);
            line = line.trim();
            //GAMELIST
            if(line.startsWith("LIST")){
                line = line.replace("LIST [", "");
                line = line.replace("]", "");
                command.put("GAMELIST", parseValues(line));
                return command;
            //MATCH
            }else if(line.startsWith("MATCH")){
                line = line.replace("MATCH {", "");
                line = line.replace("}", "");
                command.put("MATCH", parseValues(line));
                return command;
            //MOVE
            }else if(line.startsWith("MOVE")){
                line = line.replace("MOVE {", "");
                line = line.replace("}", "");                               
                command.put("MOVE", parseValues(line));
                return command;
            // TURN
            }else if(line.startsWith("YOURTURN")){
                line = line.replace("YOURTURN {", "");
                line = line.replace("}", "");
                command.put("YOURTURN", parseValues(line));
                return command;
            //Draw
            }else if(line.startsWith("DRAW")){
                line = line.replace("DRAW {", "");
                line = line.replace("}", "");
                command.put("DRAW", parseValues(line));
                return command;
            //Loss
            }else if(line.startsWith("LOSS")){
                line = line.replace("LOSS {", "");
                line = line.replace("}", "");
                command.put("LOSS", parseValues(line));
                return command;
            // WIN
            }else if(line.startsWith("WIN")){
                line = line.replace("WIN {", "");
                line = line.replace("}", "");
                command.put("WIN", parseValues(line));
                return command;
            }
        }else if(line.startsWith("ERR")){
            line = line.substring(3);
            line = line.trim();
            //Duplicate name exists
            if(line.startsWith("Duplicate")){
                String[] array = new String[1];
                array[0] = "Duplicate name exists";
                command.put("ERR", array);
                return command;
            }
        }
        return null;
    }
    
    public String[] parseValues(String line){
        String[] values = line.split(",");
        for(int i=0;i<values.length;i++){
            // whitespace verwijderen
            values[i] = values[i].substring(values[i].indexOf("\"")+1,values[i].lastIndexOf("\"")).trim();
        }
        return values;
    }

}
